home *** CD-ROM | disk | FTP | other *** search
/ Cream of the Crop 22 / Cream of the Crop 22.iso / program / ctlib100.zip / INSTALL.LZH / TABLE2.PAS < prev    next >
Pascal/Delphi Source File  |  1996-10-12  |  6KB  |  213 lines

  1. {**************************************************************************}
  2. {*  BitSoft Development, L.L.C.                                           *}
  3. {*  Copyright (C) 1995, 1996 BitSoft Development, L.L.C.                  *}
  4. {*  All rights reserved.                                                  *}
  5. {*  Containers Library demo                                               *}
  6. {**************************************************************************}
  7.  
  8. program Table2;
  9.  
  10. {$X+}
  11.  
  12. { This program demonstrates how to create a field structure, use it to
  13. initialize a TObjectTable, insert an object, and return results.  It contains
  14. all examples for object tables described in the documentation chapter on
  15. tables. }
  16.  
  17. uses
  18.   Objects, Crt, BsdTest, ctTypes, ctFields, ctTables;
  19.  
  20. const
  21.   TempFile = 'test.dat';
  22.   BufferSize = 2048;
  23.   OpenedFromDisk: Boolean = False;
  24.  
  25. var
  26.   FieldStructure: PFieldStructure;
  27.   Table: PTable;
  28.  
  29. type
  30.   PAddress = ^TAddress;
  31.   TAddress = Object(TObject)
  32.     Line1: PString;
  33.     Line2: PString;
  34.     City: PString;
  35.     State: PString;
  36.     constructor Init (ALine1, ALine2, ACity, AState: string);
  37.     constructor Load (var S: TStream);
  38.     destructor Done; virtual;
  39.     procedure Store (var S: TStream); virtual;
  40.   end;  { of TAddress }
  41.  
  42. constructor TAddress.Init (ALine1, ALine2, ACity, AState: string);
  43. begin
  44.   if not TObject.Init then
  45.     Fail;
  46.   Line1 := NewStr(ALine1);
  47.   Line2 := NewStr(ALine2);
  48.   City := NewStr(ACity);
  49.   State := NewStr(AState);
  50. end;
  51.  
  52. constructor TAddress.Load (var S: TStream);
  53. begin
  54.   if not TObject.Init then
  55.     Fail;
  56.   Line1 := S.ReadStr;
  57.   Line2 := S.ReadStr;
  58.   City := S.ReadStr;
  59.   State := S.ReadStr;
  60. end;
  61.  
  62. destructor TAddress.Done;
  63. begin
  64.   DisposeStr(Line1);
  65.   DisposeStr(Line2);
  66.   DisposeStr(City);
  67.   DisposeStr(State);
  68.   TObject.Done;
  69. end;
  70.  
  71. procedure TAddress.Store (var S: TStream);
  72. begin
  73.   S.WriteStr(Line1);
  74.   S.WriteStr(Line2);
  75.   S.WriteStr(City);
  76.   S.WriteStr(State);
  77. end;
  78.  
  79. const
  80.   RAddress: TStreamRec = (
  81.     ObjType: 2099;
  82.     VmtLink: Ofs(TypeOf(TAddress)^);
  83.     Load: @TAddress.Load;
  84.     Store: @TAddress.Store);
  85.  
  86.  
  87. function AddressFieldStructure: PFieldStructure;
  88. var
  89.   FieldStructure: PFieldStructure;
  90.   Field: PField;
  91.   Name: TFieldName;
  92.   i: Integer;
  93. begin
  94.   FieldStructure := New(PFieldStructure,Init(4,1));
  95.   if (FieldStructure <> nil) then
  96.   begin
  97.     for i := 1 to 4 do
  98.     begin
  99.       case i of
  100.         1: Name := 'Line1';
  101.         2: Name := 'Line2';
  102.         3: Name := 'City';
  103.         4: Name := 'State';
  104.       end;
  105.       Field := New(PField, Init(Name, ftPString, 50, 0));
  106.       if (Field <> nil) then
  107.         FieldStructure^.Insert(Field)
  108.       else Error('Out of memory.');
  109.     end;
  110.   end;
  111.   AddressFieldStructure := FieldStructure;
  112. end;
  113.  
  114.  
  115. procedure InsertAddresses;
  116. var
  117.   Address: PAddress;
  118. begin
  119.   Address := New(PAddress,Init('Mickey Mouse', 'Disney World',
  120.                                'Orlando', 'Florida'));
  121.   if Address = nil then
  122.     Error('Out of memory.  Could not create address record.');
  123.   Table^.Insert(Address);
  124. end;
  125.  
  126. procedure ShowAddresses;
  127. var
  128.   RecNo: LongInt;
  129.   procedure ShowAddress (Address: PAddress); far;
  130.   begin
  131.     WriteLn('Record Number = ',RecNo);
  132.     with Address^ do
  133.     begin
  134.       WriteLn('   ',Line1^);
  135.       WriteLn('   ',Line2^);
  136.       WriteLn('   ',City^,', ',State^);
  137.     end;
  138.     WriteLn;
  139.     Inc(RecNo);
  140.   end;
  141. begin
  142.   with Table^ do
  143.   begin
  144.     if not OpenedFromDisk then
  145.     begin
  146.       WriteLn('Table''s Field Structure');
  147.       Structure^.ShowInfo(OutPut);
  148.       WriteLn;
  149.     end;
  150.     RecNo := 0;
  151.     ForEach(@ShowAddress);
  152.   end;
  153. end;
  154.  
  155. var
  156.   F: File;  { just used to delete table so we don't litter your disk }
  157.   Size: LongInt;
  158.   Stream: PStream;
  159. begin
  160.   ClrScr;
  161.     { Don't forget to register all the objects! }
  162.   RegisterType(RField);
  163.   RegisterType(RFieldStructure);
  164.   RegisterType(RAddress);
  165.   Size := MemAvail;
  166.   FieldStructure := AddressFieldStructure;
  167.   if (FieldStructure = nil) then
  168.     Error('Error creating field structure.');
  169.   Stream := New(PBufStream, Init(TempFile, stCreate, 2048));
  170.   Table := New(PObjectTable, Init(FieldStructure, Stream));
  171.   if (Table = nil) then
  172.   begin
  173.       { Caution!!!! Don't dispose of the table structure if table
  174.         initialization was successful.  It is used and will be disposed of by
  175.         the table. }
  176.     Dispose(FieldStructure, Done);
  177.     Error('Error constructing table.');
  178.   end;
  179.   WriteLn('Table created successfully.');
  180.   InsertAddresses;
  181.   WriteLn('Addresses inserted successfully.');
  182.   WriteLn;
  183.   ShowAddresses;
  184.   WriteLn('Closing table.');
  185.   Dispose(Table, Done);
  186.     { Tables don't dispose of the stream on which they are stored so that the
  187.       stream can be used for other purposes within the application.  You must
  188.       explicitly dispose of the table's stream when you are finished using it
  189.       to prevent a memory leak and ensure all data is flushed from the
  190.       stream's buffers. }
  191.   Dispose(Stream, Done);
  192.   WriteLn('Reopening table.');
  193.   Stream := New(PBufStream, Init(TempFile, stOpen, BufferSize));
  194.   Table := New(PObjectTable, Open(Stream));
  195.   if (Table = nil) then
  196.     Error('Error opening table.')
  197.   else begin
  198.     WriteLn('Opened table successfully.');
  199.     OpenedFromDisk := True;
  200.   end;
  201.   WriteLn;
  202.   ShowAddresses;
  203.   Dispose(Table, Done);
  204.   Dispose(Stream, Done);
  205.     { remove the table }
  206.   Assign(F, TempFile);
  207.   {$I-}
  208.   Erase(F);
  209.   {$I+}
  210.   if (Size <> MemAvail) then
  211.     WriteLn('Memory leak.');
  212.   ReadLn;
  213. end.